import java.util.Arrays;

public class MyArraylist {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_VALUE = 10;

    public MyArraylist() {
        this.elem = new int[DEFAULT_VALUE];
    }
    //顺序表长度 - size
    public int size(){
        return this.usedSize;
    }
    //遍历顺序表- display
    public void display(){
        for (int i = 0; i < size(); i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    //判满 - isFull
    public boolean isFull(){
        if(size()>=this.elem.length){
            return true;
        }
        return false;
    }
    //增加元素及在pos位置增加元素
    public void add(int data){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
        }
        this.elem[this.usedSize] = data;
        usedSize++;
    }
    public void add(int pos,int data) throws PosWrongfulException{
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
        }
        if(pos<0||pos>this.usedSize){
            throw new PosWrongfulException("pos位置不合法异常！");
        }
        for (int i = this.usedSize-1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        usedSize++;
    }
    //是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < size(); i++) {
            if (this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }
    //查找某个元素的下标
    public int indexOf(int toFind){
        for (int i = 0; i < size(); i++) {
            if(this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    //判空 - isEmpty
    public boolean isEmpty(){
        return size() == 0;
    }
    //删除第一次出现的某个元素 - remove
    public void remove(int key){
        //判不判空都行
        if(isEmpty()){
            throw new EmptyException("顺序表为空异常!");
        }
        int index = indexOf(key);
        if(index == -1){
            System.out.println("没有这个数字！");
            return;
        }
        for (int i = index; i < size()-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        usedSize--;
    }
    //获得pos位置的元素 - get
    public int get(int pos){
        //判空有没有都行
        if(isEmpty()){
            throw new EmptyException("顺序表为空异常!");
        }
        if(pos<0||pos>=size()){
            throw new PosWrongfulException("指针不合法异常！");
        }
        return this.elem[pos];
    }
    //将pos位置的元素更新为value - set
    public void set(int pos,int value){
        if(isEmpty()){
            throw new EmptyException("顺序表为空异常!");
        }
        if(pos<0||pos>=size()){
            throw new PosWrongfulException("指针不合法异常！");
        }
        this.elem[pos] = value;
    }
    //清空顺序表
    public void clear(){
        this.usedSize = 0;
    }
}
